package ui.graphic;

import Modelos.ModeloCartas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.border.Border;

/**
 *
 * @author gustavodinis
 */
public class MenuCartas extends JPanel implements Observer {

    private Image background;
    private URL backPath;
    Image card, card2, card3, card4, card5, card6;
    URL cardPath, card2Path, card3Path, card4Path, card5Path, card6Path;
    Border blackline;
    ArrayList <Rectangle2D> cards; 

    ModeloCartas mC;

    public MenuCartas(ModeloCartas m) {
        this.mC = m;
        cards = new ArrayList();
        m.addObserver(this);
        setLayout(new GridLayout(3, 2));
        setSize(250, 671);
        setPreferredSize(new Dimension(250, 671));
        setFont(new Font("MontereyMediumFLF", Font.PLAIN, 15));
        setBackground(Color.WHITE);
        loadResources();
        motionList();
    }
    
    public void loadResources(){
        if (mC.getAllBoardDeck() != null) {
            cardPath = getClass().getResource("resources/images/" + mC.getAllBoardDeck().get(0).getNome() + ".jpg");
            card2Path = getClass().getResource("resources/images/" + mC.getAllBoardDeck().get(1).getNome() + ".jpg");
            card3Path = getClass().getResource("resources/images/" + mC.getAllBoardDeck().get(2).getNome() + ".jpg");
            card4Path = getClass().getResource("resources/images/" + mC.getAllBoardDeck().get(3).getNome() + ".jpg");
            card5Path = getClass().getResource("resources/images/" + mC.getAllBoardDeck().get(4).getNome() + ".jpg");
            card6Path = getClass().getResource("resources/images/" + mC.getAllBoardDeck().get(5).getNome() + ".jpg");

            try {
                card = ImageIO.read(cardPath);
                card2 = ImageIO.read(card2Path);
                card3 = ImageIO.read(card3Path);
                card4 = ImageIO.read(card4Path);
                card5 = ImageIO.read(card5Path);
                card6 = ImageIO.read(card6Path);

            } catch (IOException ex) {
                System.out.println("imagem erro");
            }
        }
    }

    @Override
    public void update(Observable o, Object arg) {
        loadResources();
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;

        int halfWidth, threeHeight;
        halfWidth = (int) getSize().getWidth() / 2;
        threeHeight = (int) getSize().getHeight() / 3;
        cards.clear();
        g2d.drawImage(card, 0, 0, halfWidth, threeHeight, this);
        cards.add(new Rectangle2D.Float(0, 0, halfWidth, threeHeight));
        
        g2d.drawImage(card2, halfWidth, 0, halfWidth, threeHeight, this);
        cards.add(new Rectangle2D.Float(halfWidth, 0, halfWidth, threeHeight));
        
        g2d.drawImage(card3, 0, threeHeight, halfWidth, threeHeight, this);       
        cards.add(new Rectangle2D.Float(0, threeHeight, halfWidth, threeHeight));

        g2d.drawImage(card4, halfWidth, threeHeight, halfWidth, threeHeight, this);
        cards.add(new Rectangle2D.Float(halfWidth, threeHeight, halfWidth, threeHeight));
        
        g2d.drawImage(card5, 0, threeHeight * 2, halfWidth, threeHeight, this);
        cards.add(new Rectangle2D.Float(0, threeHeight*2, halfWidth, threeHeight));
        
        g2d.drawImage(card6, halfWidth, threeHeight * 2, halfWidth, threeHeight, this);
        cards.add(new Rectangle2D.Float(halfWidth, threeHeight*2, halfWidth, threeHeight));

    }
    public void motionList() {

        final MouseAdapter compra;
        compra = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                for (int i = 0; i < cards.size(); i++) {
                    if(cards.get(i).contains(e.getPoint())){
                        mC.buyCard(i);
                    }
                }
            }
        };
        addMouseListener(compra);
    }

}
